import type { UserModelState } from '@/models/userModel';
import { qqBinding } from '@/services/user';
import useRequest from '@ahooksjs/use-request';
import type { ConnectRC } from '@umijs/max';
import { connect, history, useSearchParams } from '@umijs/max';
import { message } from 'antd';
import { forwardRef, useEffect } from 'react';
import styles from './index.less';

interface Props {
  refInstance: any;
  usermodel: UserModelState;
}

const Index: ConnectRC<Props> = () => {
  const [urlParams] = useSearchParams();
  const { run: qqBindingRun } = useRequest(qqBinding, {
    manual: true,
    onSuccess: async (res) => {
      const { code, message: msg } = res;
      if (code === 200) {
        message.success('绑定成功');
        history.push('/plaza');
      } else {
        message.error(msg);
      }
    },
  });

  useEffect(() => {
    const state = urlParams.get('state');
    const code = urlParams.get('code');
    if (state && code) {
      qqBindingRun({
        state: state,
        code: code,
      });
    }
  }, []);
  return <div className={styles.qqBindWrap}>正在绑定中...</div>;
};

export default connect(({ usermodel }: { usermodel: UserModelState }) => ({
  usermodel,
}))(forwardRef((props: any, ref) => <Index {...props} refInstance={ref} />));
